<?php
  class UtilComponent extends Component {
    //dateIn mm/dd/yyy
    public function formatDate($dateIn, $mdyFormat=false) {
      $retVal = "";
      if(!empty($dateIn)){
        $date = new DateTime($dateIn);
        if(!$mdyFormat) {
          //$retVal = DateTime::createFromFormat('m/d/Y', $dateIn)->format('Y-m-d');
          $parts = explode('/', $dateIn);
          $retVal = $date->format('Y-m-d');
        } else {
          //$retVal = DateTime::createFromFormat('Y-m-d H:i:s', $dateIn)->format('m/d/Y');
          $retVal = $date->format('m/d/Y');
        }
      }
      return $retVal;
    }
    public function deleteFile($filePathIn) {
      $retVal = false;
      if(!empty($filePathIn)) {
        $retVal =unlink($filePathIn);
      }
      return $retVal;
    }

    public function cleanFileName($fileNameIn="", $removeExtension = false) {
    $retVal = $fileNameIn;
    $retVal=preg_replace("/\s+/","_",$retVal);
    $retVal=str_replace("'","",$retVal);
    if ($removeExtension) {
      if(stripos($retVal, ".")!==false) {
        $retVal = substr($retVal, 0, stripos($retVal, "."));
      }
    }
    return $retVal;
  }
  
    /* Returns the value of specific url query string parameter */
  
    public function getUrlQueryStringParameter($url, $param){
      $retVal = "";
      if(!empty($url) && !empty($param)){
        $parsedUrl = parse_url($url);
        if(!empty($parsedUrl) && !empty($parsedUrl['query']))
        $queryStringParms = $this->convertUrlQuery($parsedUrl['query']);
        $retVal = !empty($queryStringParms[$param])? $queryStringParms[$param] : "";
      }
      return $retVal;
    }

   /**
   * Returns the url query as associative array
   *
   * @param    string    query
   * @return    array    params
   */
  private function convertUrlQuery($query) {
      $queryParts = explode('&', $query);

      $params = array();
      foreach ($queryParts as $param) {
          $item = explode('=', $param);
          $params[$item[0]] = $item[1];
      }

      return $params;
  }

  }
?>